package com.wits.qso.web.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wits.platform.web.controller.BaseController;
import com.wits.qso.common.enumerate.BusinessType;
import com.wits.qso.entity.Aid;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.Cls;
import com.wits.qso.entity.CreContent;
import com.wits.qso.entity.HomeworkH;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.entity.Student;
import com.wits.qso.entity.Year;
import com.wits.qso.service.AttachmentService;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.ClsService;
import com.wits.qso.service.CreContentService;
import com.wits.qso.service.CroomService;
import com.wits.qso.service.MaterialHService;
import com.wits.qso.service.MaterialdService;
import com.wits.qso.service.StudentService;
import com.wits.qso.service.YearService;

@Controller
@RequestMapping(value="qso/mybtable")
public class BtableMyController extends BaseController {
	/**
	 * 注入studentService
	 */
	@Autowired
	private StudentService studentService;
	@Autowired
	private BtableDaoService btableDaoService;
	@Autowired
	private ClsService clsService;
	@Autowired
	private YearService yearService;
	@Autowired
	private CreContentService creContentService = null;
	@Autowired
	private MaterialdService materialdService = null;
	@Autowired
	private MaterialHService materialHService=null;
	@Autowired
	private CroomService croomService = null;
	@Autowired
	private AttachmentService attachmentService = null;
	/**
	 * 我的课表
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toList")
	public String toMybtable(Model model){
		Student stu=studentService.findByUserId(getCurrentUserId());
		Cls cls=clsService.getOne(stu.getClsId());
		
		Calendar cal=Calendar.getInstance();
		Year year = yearService.findNow( cal.getTime() ); 
		
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		Date monday=cal.getTime();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		Date sunday=cal.getTime();
		
		String info=cls.getDept().getTitle()+cls.getGrade().getGradeTitle()+cls.getClsTitle()+"   "+year.getTitle();
		
		model.addAttribute( "currentWeek", getCurrentWeeksOfYear());
		model.addAttribute( "totalWeeks", getWeeksOfThisYear());
		model.addAttribute("jsons", btableDaoService.getEventsJson(cls,monday,sunday));
		model.addAttribute("info", info);
		
		return "qso/btablemy/list";
	}
	
	@RequestMapping(value="/refresh")
	@ResponseBody
	public String Refresh(Model model,@RequestParam String currentWeek){
		Calendar cal=Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		int amount=Integer.parseInt(currentWeek)-getCurrentWeeksOfYear();
		cal.add(Calendar.WEEK_OF_YEAR, amount);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		Date monday=cal.getTime();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		Date sunday=cal.getTime();
		Student stu=studentService.findByUserId(getCurrentUserId());
		Cls cls=clsService.getOne(stu.getClsId());
		String json=btableDaoService.getEventsJson(cls,monday,sunday);
		return json;
	}
	
	@RequestMapping(value = "/view/view")
	public String toView(Model model, @RequestParam String id) {
		if (StringUtils.isNotBlank( id )) {
			Btable btable=null;
			Aid aid=null;
			CreContent creContent=null;
			HomeworkH homeworkH=null;
			
			btable = btableDaoService.findByclaIdAndId(id, clsService.getOne(studentService.findByUserId(getCurrentUserId()).getClsId()));
			aid = creContentService.findAidByBtable( btable );
			creContent = creContentService.findCreContentByBtable( btable );
			homeworkH = creContentService.findHomeworkHByBtable( btable );
			
			if(btable==null) 
				btable=new Btable();
			if(aid==null) 
				aid=new Aid();
			if(creContent==null) 
				creContent=new CreContent();
			if(homeworkH==null) 
				homeworkH=new HomeworkH();
			
			model.addAttribute( "aid", aid );
			model.addAttribute( "creContent", creContent );
			model.addAttribute( "homeworkH", homeworkH );
			//model.addAttribute( "materialD", creContent.getMtrdId() );
			List<MaterialD> materialDs = creContentService.findMaterialDByBtable( btable );
			model.addAttribute( "materialDs", null == materialDs ? null : materialDs );	
			model.addAttribute( "btable", btable );
			model.addAttribute( "aidAttachments", attachmentService.findByBusiness( aid.getId(),  BusinessType.AID.getValue() ) );
			model.addAttribute( "homeworkHAttachments", attachmentService.findByBusiness( homeworkH.getId(),  BusinessType.HWKH.getValue() ) );
		}

		return "qso/btablemy/form";
	}
	
//	private static String dateToString(Date date) {   
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
//		String startStr = sdf.format(date);   
//		String str = startStr.split(" ")[0] + "T" + startStr.split(" ")[1];   
//		return str + ".000+10:00";   
//	}  
	
	protected int getCurrentWeeksOfYear() {
		int weeks = 1;
		Year year = yearService.findNow( Calendar.getInstance().getTime() );
		
		if (null != year) {
			Calendar current = Calendar.getInstance();
			current.set( Calendar.HOUR_OF_DAY, 0 );
			current.set( Calendar.MINUTE, 0 );
			current.set( Calendar.SECOND, 0 );
			current.set( Calendar.MILLISECOND, 0 );
			
			Calendar start = Calendar.getInstance();
			start.setTime( year.getDateF() ); 
			
			long diff = current.getTimeInMillis() - start.getTimeInMillis();
			int dayCount = new Float( (float) diff / (24 * 60 * 60 * 1000) ).intValue();

			weeks = dayCount / 7 + ( 0 != (dayCount % 7) ? 1 : 0 );
		}
		
		return weeks;
	}
	
	protected int getWeeksOfThisYear() {
		Year year = yearService.findNow( Calendar.getInstance().getTime() );
		int weeks = 1;
		
		if (null != year) {
			Calendar start = Calendar.getInstance();
			Calendar end = Calendar.getInstance();
			start.clear();
			end.clear();
			
			start.setTime( year.getDateF() );
			end.setTime( year.getDateT() );

			long diff = end.getTimeInMillis() - start.getTimeInMillis();

			int dayCount = new Float( (float) diff / (24 * 60 * 60 * 1000) ).intValue();

			weeks = dayCount / 7 + ( 0 != (dayCount % 7) ? 1 : 0 );
		}
		
		return weeks;
	}

}
